package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.ILocalQuerySceneOrderAttrService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OrderInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 订单信息模块
 */
@Service
public class OrderInfoModuleHandler extends AbstractModuleHandler {

    @Autowired
    ILocalQuerySceneOrderAttrService querySceneOrderAttrService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        OrderInfoVO orderInfoVO = new OrderInfoVO();

        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String custId = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId()).getCustId();
        String offerInstId = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId())
            .getRootInstId();
        List<KiteAttr> offerAttrs = querySceneInstService.getOfferInst(custId, offerInstId).listAttrs();
        offerAttrs = offerAttrs.stream().filter((attr) -> KeyConsts.PROD_ATTR_SHOW_TYPE_O.equals(attr.getShowType()))
            .collect(Collectors.toList());
        /*List<InstAttrDTO> orderAttrConfigs = querySceneOrderAttrService.listOrderAttrs(context.getAcceptRequest().getSceneInstId(), KeyConsts.ORDER_ATTR_OBJECT_TYPE).getResultObject();

        List<CommonAttrVO> orderAttrs =  AssemblyAttrDtoUtils.assemblyVO(orderAttrConfigs);*/
        List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(offerAttrs);

        orderInfoVO.setOrderAttr(instAttrDTOList);
        orderInfoVO.setOfferInstId(offerInstId);
        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        sceneAccountInfoVo.setOrderInfo(orderInfoVO);
        
        if (KiteListUtils.isEmpty(instAttrDTOList)) {
            return previewSceneVo(context, sceneModule);
        }

        return Collections.singletonList(orderInfoVO);
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        Map<String, Object> orderInfo = querySceneInstService.getExtParameterValue(context.getSceneVo().getSceneInstId(), "orderInfoMap");
        String custId = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId()).getCustId();
        String offerInstId = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId())
            .getRootInstId();
        List<KiteAttr> offerAttrs = querySceneInstService.getOfferInst(custId, offerInstId).listAttrs();
        offerAttrs = offerAttrs.stream().filter((attr) -> KeyConsts.PROD_ATTR_SHOW_TYPE_O.equals(attr.getShowType()))
            .collect(Collectors.toList());
        
        if (KiteMapUtils.isNotEmpty(orderInfo)) {
            Map<String, Object> orderMap = (Map<String, Object>) Optional.ofNullable(orderInfo.get("order")).orElse(new HashMap<>());
            Map<String, Object> requestMap = (Map<String, Object>) Optional.ofNullable(orderInfo.get("request")).orElse(new HashMap<>());
            Map<String, Object> acceptMap = (Map<String, Object>) Optional.ofNullable(orderInfo.get("accept")).orElse(new HashMap<>());
            List<Map<String, Object>> userMap = (List<Map<String, Object>>) Optional.ofNullable(orderInfo.get("users")).orElse(new ArrayList());
            
            // 订单编号
            offerAttrs.add(getKiteAttr("orderId", KiteMapUtils.getString(orderMap, "orderId"), KiteMapUtils.getString(orderMap, "orderId"), "订单编号"));
            // 订单类型
            offerAttrs.add(getKiteAttr("orderTypeName", KiteMapUtils.getString(orderMap, "orderTypeName"), KiteMapUtils.getString(orderMap, "orderTypeName"), "订单类型"));
            // 业务号码
            offerAttrs.add(getKiteAttr("serialNumber", KiteMapUtils.getString(userMap.get(0), "serialNumber"), KiteMapUtils.getString(userMap.get(0), "serialNumber"), "业务号码"));
            // 订单来源
            offerAttrs.add(getKiteAttr("sourceMallName", KiteMapUtils.getString(requestMap, "sourceMallName"), KiteMapUtils.getString(requestMap, "sourceMallName"), "订单来源"));
            // 受理时间
            offerAttrs.add(getKiteAttr("startTime", KiteMapUtils.getString(orderMap, "startTime"), KiteMapUtils.getString(orderMap, "startTime"), "受理时间"));
            // 受理部门
            offerAttrs.add(getKiteAttr("departmentName", KiteMapUtils.getString(acceptMap, "departmentName"), KiteMapUtils.getString(acceptMap, "departmentName"), "受理部门"));
            // 受理员工
            offerAttrs.add(getKiteAttr("name", KiteMapUtils.getString(acceptMap, "name"), KiteMapUtils.getString(acceptMap, "name"), "受理员工"));
        }
        
        //设置预览模式
        offerAttrs.forEach((attr) -> {
            attr.setAttrValueTypeId("TT");
        });
        OrderInfoVO orderInfoVO = new OrderInfoVO();

        List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(offerAttrs);

        orderInfoVO.setOrderAttr(instAttrDTOList);
        orderInfoVO.setOfferInstId(offerInstId);
        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        sceneAccountInfoVo.setOrderInfo(orderInfoVO);
        return Collections.singletonList(orderInfoVO);
    }
    
    private KiteAttr getKiteAttr(String fieldName, String value, String valueDesc, String cname) {
        KiteAttr kiteAttr = new KiteAttr();
        kiteAttr.setFieldName(fieldName);
        kiteAttr.setValue(value);
        kiteAttr.setValueDesc(valueDesc);
        kiteAttr.setCname(cname);
        return kiteAttr;
    }
    
}
